package com.yxjme.redpackagelibrary;

import android.os.CountDownTimer;
import android.util.Log;

public class CountDownUtil {


    /**
     * 倒计时时间间隔
     */
    private int speed= 1000;
    /**
     * 倒计时
     */
    private int time;
    /**
     * 暂停
     */
    private  boolean isPause ;

    /**
     * 回调
     */
    private AddCountDownListener addCountDownListener;


    public CountDownUtil addCountDownListener(AddCountDownListener addCountDownListener) {
        this.addCountDownListener = addCountDownListener;
        return CountDownUtil.this ;
    }

    public void setPause(boolean pause) {
        isPause = pause;
    }

    /**
     * 秒
     * @param time
     * @return
     */
    public CountDownUtil setTimeSeconds(int time) {
        this.time = time *1000;
        return CountDownUtil.this ;
    }


    /**
     * 毫秒
     * @param time
     * @return
     */
    public CountDownUtil setTimeMillis(int time){
        this.time = time;
        return CountDownUtil.this ;
    }



    /**
     * 倒计时间隔时间
     *
     * @param speed
     */
    public CountDownUtil setSpeed(int speed) {
        this.speed = speed;
        return CountDownUtil.this ;
    }



    /**
     * 重置
     */
    public void reset(){
        time = 0 ;
        isPause = false ;
        if (countDownTimer!=null){
            countDownTimer.cancel();
            if (addCountDownListener!=null){
                addCountDownListener.onReset();
            }
        }
    }



    private CountDownTimer countDownTimer;

    public void startCountDown(){

        if (countDownTimer!=null){
            countDownTimer.cancel();
            countDownTimer = null ;
        }

        if (time>0){
            countDownTimer = new CountDownTimer(time, speed) {
                @Override
                public void onTick(long l) {
                    int current = (int) (l/1000);
                    Log.v("=======countDown=======","s="+current);
                    if (addCountDownListener!=null)
                        addCountDownListener.onCountDowning(getTimeBean(current));
                }

                @Override
                public void onFinish() {
                    Log.v("=======countDown=======","onFinish");
                    if (addCountDownListener!=null)
                        addCountDownListener.onCountComplete();
                }
            }.start();
        }
    }


   public static class TimeBean{
       public String h ;//时
       public String m ;//分
       public String sc ;//秒
       public TimeBean( String h, String m, String sc) {
            this.h = h;
            this.m = m;
            this.sc = sc;
        }
    }



    public TimeBean getTimeBean(long t){
        int hour = (int) (t / (60 * 60));
        int minutes = (int) (t % (60 * 60)) / 60;
        int seconds = (int) (t % (60 * 60)) % 60;
        return formatTime(hour,minutes,seconds) ;
    }



    /**
     * @param h
     * @param m
     * @param s
     * @return
     */
    private TimeBean formatTime(int h,int m,int s){
        String Hour;
        String Minute;
        String Second;

        if (h>0){
            if (h<10){
                Hour = "0"+h;
            }else {
                Hour = h+"";
            }
        }else {
            Hour = "00";
        }

        if (m>0){
            if (m<10){
                Minute = "0"+m;
            }else {
                Minute =  m+"";
            }
        }else {
            Minute = "00";
        }

        if (s>0){
            if (s<10){
                Second = "0"+s;
            }else {
                Second = ""+s;
            }
        }else {
            Second = "00";
        }

        TimeBean timeData=new TimeBean(Hour,Minute,Second);
        return timeData;
    }
}
